package cn.com.kblog.controller.admin;


import java.io.File;
import java.util.HashMap;
import java.util.Map;

import cn.com.kblog.common.Constant;
import cn.com.kblog.common.Result;
import cn.com.kblog.model.EmailVerify;
import cn.com.kblog.model.User;
import cn.com.kblog.util.AppConfigUtil;
import cn.com.kblog.util.LoginUtils;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StringKit;
import com.jfinal.log.Logger;
/**
 * 
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-12-2下午3:19:25
 */
@ControllerBind(controllerKey = "/admin",viewPath="/WEB-INF/pages/admin")
public class AdminController extends Controller{
	private static final Logger log = Logger.getLogger(AdminController.class);
	public static final String ADMIN_INDEX ="/admin/blog/list";
	
	/**
	 *登陆页面 
	 */
	public void index(){
		if(LoginUtils.isLogin()){
			log.info("----user---"+LoginUtils.getLoginUser().getStr(User.EMAIL)+"---login...");
			log.info("--redirect--"+ADMIN_INDEX);
			try {
				redirect(ADMIN_INDEX);
				return;
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			
		}
		render("login.html");
	}
	
	/**
	 * 登录
	 */
	public void login(){
		String username = getPara("username");
		String password = getPara("password");
		String remenber = getPara("remenber");
		boolean flag = false;
		if("1".equals(remenber)){
			flag = LoginUtils.login2remenber(username, password);
		}else{
			flag = LoginUtils.login(username, password);
		}
		if(flag){
			redirect(ADMIN_INDEX);
		}else{
			keepPara();
			setAttr(Constant.RESULT_KEY, new Result(false, "账号或密码不正确"));
			render("login.html");
		}
		
	}
	
	public void logout(){
		LoginUtils.logout();
		setAttr(Constant.RESULT_KEY, new Result(true,"退出成功"));
		setAttr("jumptime", "3000");
		render("message.html");
	}
	
	
	/**
	 * 邮箱验证
	 */
	public void email_verify(){
		String verifyCode = getPara(Constant.EMAIL_VERIFY_CODE);
		if(StringKit.isBlank(verifyCode)){
			setAttr(Constant.RESULT_KEY, new Result(false, "信息错误"));
			render("message.html");
			return;
		}
		EmailVerify verify = EmailVerify.dao.getByCode(verifyCode);
		if(verify==null){
			setAttr(Constant.RESULT_KEY, new Result(false, "链接失效，请重新操作或联系管理员"));
			render("message.html");
			return;
		}
		keepPara();
		
		render("password.html");
	}
	
	
	/**
	 * 保存验证成功后修改的密码
	 */
	public void password_save(){
		String verifyCode = getPara(Constant.EMAIL_VERIFY_CODE);
		if(StringKit.isBlank(verifyCode)){
			setAttr(Constant.RESULT_KEY, new Result(false, "信息错误，请重新操作或联系管理员"));
			render("message.html");
			return;
		}
		EmailVerify verify = EmailVerify.dao.getByCode(verifyCode);
		if(verify==null){
			setAttr(Constant.RESULT_KEY, new Result(false, "信息错误，请重新操作或联系管理员"));
			render("message.html");
			return;
		}
		Integer userId = verify.getInt(EmailVerify.USER_ID);
		User user = getModel(User.class,Constant.BEAN_KEY);
		user.set("id", userId);
		user.set(User.EMAIL_VERIFY, User.EMAIL_VERIFY_YES);
		Result result = user.saveOrUpdate();
		if(result.isSuccess()){
			verify.delete();
		}
		setAttr(Constant.RESULT_KEY, result.isSuccess()?result.success("完成验证修改成功！"):result.error("修改失败,，请重新操作或联系管理员"));
		render("message.html");
	}
	
	
	/**
	 * 忘记密码
	 */
	public void forgot_password(){
		render("forgot_password.html");
	}
	
	public void send_email_verify(){
		
		String email = getPara("email");
		User user = User.dao.findByEmail(email);
		if(user==null){
			setAttr(Constant.RESULT_KEY, new Result(false,"此邮箱没有注册，请联系管理员！"));
			setAttr("jumptime","3000");
			setAttr("jumpurl","admin/forgot_password");
			render("message.html");
		}
		
		String subject = "K博客验证邮件";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("domain", AppConfigUtil.get("domain.name"));
		params.put(User.NICK_NAME, email);
		params.put("verify_code", EmailVerify.dao.createVerify(user.getID()));
		String dir = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + "template";
		String tempname = "email_verify.ftl";
		//TODO 暂时先去忘记密码功能
		//SendHtmlEmail4BAE.sendTemplateEmail(subject, email, params,dir, tempname);
		
		setAttr(Constant.RESULT_KEY, new Result(true,"已发送验证邮件，请注意查看邮箱！"));
		render("message.html");
	}
}
